﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Snowy.Extensions
{
    public class ThirdPartyFonts
    {
        private readonly IMemoryCache _memoryCache;
        private readonly IWebHostEnvironment _env;
        private readonly AppSettings _settings;
        private readonly ILogger<ThirdPartyFonts> _logger;
        PrivateFontCollection _fonts;
        public ThirdPartyFonts(IMemoryCache memoryCache, IWebHostEnvironment env, IOptions<AppSettings> options, ILogger<ThirdPartyFonts> logger)
        {
            _memoryCache = memoryCache;
            _env = env;
            _settings = options.Value;
            _logger = logger;
        }
        public void LoadThirdPartyFonts(byte[] buffer)
        {
            var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            try
            {
                var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
                if (_fonts != null) _fonts = new PrivateFontCollection();
                _fonts.AddMemoryFont(ptr, buffer.Length);

            }
            finally
            {
                handle.Free();
            }
        }
        public Font RandThirdPartyFonts(int size, FontStyle fontStyle)
        {
            return new Font(_fonts.Families[new Random(DateTime.Now.Second).Next(0, _fonts.Families.Length)], size, fontStyle);
        }
        public Font GetThirdPartyFonts(string name, int size, FontStyle fontStyle)
        {
            return new Font(_fonts.Families.FirstOrDefault(f => f.Name == name), size, fontStyle);
        }
    }
    public static class ThirdPartyFontsExtension
    {
        public static IApplicationBuilder UseThirdPartyFont(this IApplicationBuilder app, byte[] buffer)
        {
            var tpf = app.ApplicationServices.GetService<ThirdPartyFonts>();
            tpf.LoadThirdPartyFonts(buffer);
            return app;
        }
        public static IApplicationBuilder UseThirdPartyFonts(this IApplicationBuilder app)
        {
            var tpf = app.ApplicationServices.GetService<ThirdPartyFonts>();
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷仓耳渔阳体);
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷庆科黄油体);
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷快乐体);
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷文艺体);
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷酷黑体);
            tpf.LoadThirdPartyFonts(Properties.Resources.站酷高端黑);
            return app;
        }
        public static IServiceCollection AddThirdPartyFonts(IServiceCollection services)
        {
            return services.AddSingleton<ThirdPartyFonts>();

        }
    }

}
